import axios from "axios"
import { useGlobalStore } from "../store"

let ACCOUNT_BASE_URL: string
let SIP_BASE_URL: string

if (import.meta.env.MODE === 'development2') {
    ACCOUNT_BASE_URL = "http://127.0.0.1:3000"
    SIP_BASE_URL = "http://127.0.0.1:3000"    
}else {
	ACCOUNT_BASE_URL = "https://8eiteepock.execute-api.eu-central-1.amazonaws.com/Prod"
	SIP_BASE_URL = "https://gm2r0i9dcf.execute-api.eu-central-1.amazonaws.com/Prod"
}


export async function api_login(account: string, password: string): Promise<any> {
    const url = ACCOUNT_BASE_URL + "/signin"

    return axios.post(url, {
        account: account,
        password: password
    }, {
        headers: {
            'Content-Type': 'application/json',
        }
    })
}

export async function api_get_all_orders(): Promise<any> {
    const store = useGlobalStore();
    const url = SIP_BASE_URL + "/orders";
    return axios.get(url, {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + store.token
        }
    });
}

export async function api_get_deviceItem(owner: string, deviceId: string): Promise<any> {
    const store = useGlobalStore();
    const url = SIP_BASE_URL + "/device_item?owner=" + owner + "&deviceid=" + deviceId;
    return axios.get(url, {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + store.token
        }
    });
}

export async function api_update_deviceItem(owner: string, data: any): Promise<any> {
    const store = useGlobalStore();
    const url = SIP_BASE_URL + "/device_item?owner=" + owner;
    return axios.patch(url, data, {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + store.token
        }
    });
}


export const codeToCountryMap = new Map<string, string>([
    ['AL', '355 Albania'],
    ['AR', '+54 Argentina'],
    ['AU', '+61 Australia'],
    ['AT', '+43 Austria'],
    ['BE', '+32 Belgium'],
    ['BA', '+387 Bosnia and Herzegovina'],
    ['BR', '+55 Brazil'],
    ['BG', '+359 Bulgaria'],
    ['CA', '+1 Canada'],
    ['CL', '+56 Chile'],
    ['CN', '+86 China'],
    ['CR', '+506 Costa Rica'],
    ['HR', '+385 Croatia'],
    ['CY', '+357 Cyprus'],
    ['CZ', '+420 Czech Republic'],
    ['DK', '+45 Denmark'],
    ['EE', '+372 Estonia'],
    ['FI', '+358 Finland'],
    ['FR', '+33 France'],
    ['GE', '+995 Georgia'],
    ['DE', '+49 Germany'],
    ['GR', '+30 Greece'],
    ['HU', '+36 Hungary'],
    ['IE', '+353 Ireland'],
    ['IL', '+972 Israel'],
    ['IT', '+39 Italy'],
    ['JP', '+81 Japan'],
    ['LV', '+371 Latvia'],
    ['LT', '+370 Lithuania'],
    ['LU', '+352 Luxembourg'],
    ['MX', '+52 Mexico'],
    ['NL', '+31 Netherlands'],
    ['NZ', '+64 New Zealand'],
    ['NO', '+47 Norway'],
    ['PA', '+507 Panama'],
    ['PE', '+51 Peru'],
    ['PL', '+48 Poland'],
    ['PT', '+351 Portugal'],
    ['PR', '+1 Puerto Rico'],
    ['RS', '+381 Serbia'],
    ['SG', '+65 Singapore'],
    ['SK', '+421 Slovakia'],
    ['ZA', '+27 South Africa'],
    ['ES', '+34 Spain'],
    ['SE', '+46 Sweden'],
    ['CH', '+41 Switzerland'],
    ['TH', '+66 Thailand'],
    ['GB', '+44 UK'],
    ['US', '+1 United States'],
    ['VI', '+1 US Virgin Islands']
  ]);

type FileInfo = {
    url: string,
    fileName: string,
    expired: number
};

const fileUrlCacheMap = new Map<string, FileInfo>();

export async function getFileUrl(fileName: string) {
    if (fileUrlCacheMap.has(fileName)) {
        const fileInfo: FileInfo = fileUrlCacheMap.get(fileName) as FileInfo;
        if (fileInfo.expired * 1000 > Date.now()) {
            return fileInfo.url;
        }
    }else {
        const options = {
            urlType: "download",
            filename: fileName
        }

        try {
            const res = await axios.post(SIP_BASE_URL + "/resource_url", options, {
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + useGlobalStore().token
                }
            })

            if (res.status === 200) {
                console.log("getFileUrl", res.data);
                return res.data.data
            }
        }catch (e) {
            console.log(e);
        }   
    }

    return ''
}


export async function api_del_order(ticketId: string) {
    const store = useGlobalStore();
    const url = SIP_BASE_URL + "/orders?ticketid=" + ticketId;
    return axios.delete(url, {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + store.token
        }
    });
}


export async function api_update_order(ticketId: string, status: any) {
    const store = useGlobalStore();
    const url = SIP_BASE_URL + "/order_status?ticketid=" + ticketId + "&status=" + status;
    
    return axios.patch(url, {}, {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + store.token
        }
    });
}

export async function api_update_device(owner: string, data: any) {
    const store = useGlobalStore();
    const url = SIP_BASE_URL + "/device_item?owner=" + owner;
    return axios.patch(url, data, {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + store.token
        },
        data: data
    });
}